//
//  ShowCollectionViewController.swift
//  CollectionViewUsage
//
//  Created by han on 2020/5/13.
//  Copyright © 2020 han. All rights reserved.
//

import UIKit

class WaterfallCollectionViewController: UIViewController {

    var collectionView: UICollectionView!
    var waterfallLayout: WaterfallLayout!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        commonInit()
       
    }
    
    func commonInit() {
        waterfallLayout = WaterfallLayout()
        waterfallLayout.footerHeight = 50
        waterfallLayout.headerHeight = 20
        waterfallLayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: waterfallLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TestCell.self, forCellWithReuseIdentifier: TestCell.identifiers)
        collectionView.register(TestHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: TestHeaderView.header)
        collectionView.register(TestHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: TestHeaderView.footer)
        collectionView.backgroundColor = UIColor(white: 0.5, alpha: 0.3)
        view.addSubview(collectionView)
    }
    
}

extension WaterfallCollectionViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TestCell.identifiers, for: indexPath) as! TestCell
        cell.testLab.text = " \(indexPath.section) section \(indexPath.row) item"
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        if kind == WaterfallLayout.headerKey {
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: TestHeaderView.header, for: indexPath) as! TestHeaderView
            header.testLab.text = "header view"
            header.backgroundColor = .purple
            return header
          } else if kind == WaterfallLayout.footerKey {
            let footer = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: TestHeaderView.footer, for: indexPath) as! TestHeaderView
            footer.testLab.text = "footer view"
            footer.backgroundColor = .systemBlue
            return footer
          }
          return UICollectionReusableView()
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
        
    }

}

extension WaterfallCollectionViewController: WaterfallDelegateLayout {
    func collectionView(_ collectionView: UICollectionView, _ layout: UICollectionViewLayout, columnCountForSection section: Int) -> Int {
        return section == 1 ? 3 : section == 2 ? 1 : 2
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, heightForFooterInSection section: Int) -> CGFloat {
//        return 50
//    }
//
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, heightForHeaderInSection section: Int) -> CGFloat {
//        return 20
//    }
//
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 30, height: 15)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return section == 1 ? .zero : UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
}

class TestCell: UICollectionViewCell {
  static let identifiers = "testcellssss"
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    configBaseView()
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  func configBaseView() {
    backgroundColor = .darkGray
    addSubview(testLab)
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
    testLab.frame = self.bounds
  }
  
  lazy var testLab: UILabel = {
    let tmp = UILabel()
    tmp.textColor = .white
    tmp.font = .systemFont(ofSize: 14)
    tmp.textAlignment = .center
    tmp.numberOfLines = 0
    return tmp
  }()
}

class TestHeaderView: UICollectionReusableView {
  static let header = "testHeaderViewId"
  static let footer = "testFooterViewId"
  override init(frame: CGRect) {
    super.init(frame: frame)
    configBaseView()
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  func configBaseView() {
    addSubview(testLab)
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
    testLab.frame = bounds
  }
  
  lazy var testLab: UILabel = {
    let tmp = UILabel()
    tmp.textColor = .white
    tmp.font = .systemFont(ofSize: 14)
    tmp.textAlignment = .center
    tmp.numberOfLines = 0
    return tmp
  }()
}



